package com.jyxd.web.service.assessment.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyxd.web.dao.basic.BedArrangeDao;
import com.jyxd.web.dao.basic.BedArrangePlusDao;
import com.jyxd.web.dao.patient.PatientDao;
import com.jyxd.web.data.basic.BedArrange;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.data.patient.Patient;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;
import static com.jyxd.web.util.UUIDUtil.getUUID;

@Service("bedArrangeService")
@Transactional
public class BedArrangeService {

    @Autowired
    private BedArrangeDao bedArrangeDao;

    @Resource
    private BedArrangePlusDao bedArrangePlusDao;
    
    @Resource
    private PatientDao patientDao;

    public boolean insert(BedArrange bedArrange) {
        return bedArrangeDao.insert(bedArrange);
    }

    public boolean update(BedArrange bedArrange) {
        return bedArrangeDao.update(bedArrange);
    }

    public BedArrange queryData(String id) {
        return bedArrangeDao.queryData(id);
    }

    public List<BedArrange> queryList(Map<String, Object> map) {
        return bedArrangeDao.queryList(map);
    }

    public int queryNum(Map<String, Object> map) {
        return bedArrangeDao.queryNum(map);
    }

    public List<Map<String, Object>> getBedArrangeList(Map<String, Object> map) {
        return bedArrangeDao.getBedArrangeList(map);
    }

    /**
     * 根据条件查询床位安排实体对象
     *
     * @param map
     * @return
     */
    public BedArrange queryDataByCode(Map<String, Object> map) {
        return bedArrangeDao.queryDataByCode(map);
    }

    /**
     * 根据患者id查询床位安排
     *
     * @param patientId
     * @return
     */
    public BedArrange queryDataByPatientId(String patientId) {
        return bedArrangeDao.queryDataByPatientId(patientId);
    }

    /**
     * 清空监护仪的数据
     */
    public boolean updateBedCode(String monitorCode,String tenantId) {
        return bedArrangeDao.updateBedCode(monitorCode,tenantId);
    }

    public HttpResult deviceBindingById(BedArrange bedArrange) {
        errorIfEmpty(bedArrange.getPatientId(), "患者ID不存在");
        try {
            //通过患者ID查床位号
            Patient patient = patientDao.queryData(bedArrange.getPatientId());
            //通过床号查床表
            BedArrange bedArr = bedArrangePlusDao.selectOne(new QueryWrapper<BedArrange>().eq("bed_code", patient.getBedCode()));
            if(ObjectUtils.isEmpty(bedArr)){
                //是null,就新增
                BedArrange bedArrange1 = new BedArrange();
                bedArrange1.setId(getUUID());
                bedArrange1.setBedCode(patient.getBedCode());
                bedArrange1.setMonitorCode(bedArrange.getMonitorCode());
                bedArrange1.setBindDate(new Date());
                bedArrange1.setBindingName(bedArrange.getBindingName());
                bedArrangePlusDao.insert(bedArrange1);
                return new HttpResult(200, bedArrange1,"绑定成功");
            }else{
                //通过监护仪编号，去床位查
                BedArrange bedArrange1 = bedArrangePlusDao.selectOne(new QueryWrapper<BedArrange>().eq("monitor_code", bedArrange.getMonitorCode()));
                if(ObjectUtils.isEmpty(bedArrange1)){
                    //是null,修改当前监护仪编号为这个床的
                    bedArr.setMonitorCode(bedArrange.getMonitorCode());
                    bedArr.setBindDate(new Date());
                    bedArr.setBindingName(bedArrange.getBindingName());
                    bedArrangePlusDao.update(bedArr,new QueryWrapper<BedArrange>().eq("id", bedArr.getId()));
                    return new HttpResult(200, bedArr,"绑定成功");
                }else{
                    //不是null,取消当前绑定
                    bedArrange1.setMonitorCode(null);
                    bedArrangePlusDao.update(bedArrange1,new QueryWrapper<BedArrange>().eq("id", bedArrange1.getId()));
                    //修改当前监护仪编号为这个床的
                    bedArr.setMonitorCode(bedArrange.getMonitorCode());
                    bedArr.setBindDate(new Date());
                    bedArr.setBindingName(bedArrange.getBindingName());
                    bedArrangePlusDao.update(bedArr,new QueryWrapper<BedArrange>().eq("id", bedArr.getId()));
                    return new HttpResult(200, bedArr,"绑定成功");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult(500, e.getMessage(),"绑定失败");
        }

    }

    public HttpResult deviceUnboundById(BedArrange bedArrange) {
        errorIfEmpty(bedArrange, "参数为空");
        errorIfEmpty(bedArrange.getBedCode(), "床位为空");
        //把当前床位的监护仪设置null
        try {
            bedArrange.setMonitorCode(null);
            bedArrange.setBindDate(null);
            bedArrange.setBindingName(null);
            bedArrangePlusDao.update(bedArrange,new QueryWrapper<BedArrange>().eq("bed_code", bedArrange.getBedCode()));
            return new HttpResult(200, "解绑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult(500, e.getMessage(),"绑定失败");
        }
    }

    public HttpResult getMonitorInfoById(String patientId) {
        try {
            List<BedArrange> bedArranges = bedArrangeDao.getMonitorInfoById(patientId);
            return new HttpResult(200,bedArranges,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult(500, e.getMessage(),"查询失败");
        }
    }
}
